﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using HNG.LIB.Controls;
using HNG.Portal.BLL;
using HNG.Portal.DAL;
using HNG.Portal.DAL.EntityClasses;
using HNG.Portal.DAL.FactoryClasses;
using HNG.Portal.DAL.HelperClasses;
using SD.LLBLGen.Pro.ORMSupportClasses;
using System.Web.UI.HtmlControls;
using HNG.LIB.Common;
public partial class AdminPortal_Controls_Account_User_List : System.Web.UI.UserControl
{
    string LangID = HNG.LIB.Localization.LocalizationUtils.GetCurrentLanguage();
    
    TblThanhVienManager _TblThanhVienManager = new TblThanhVienManager();
    public int PageSize
    {
        get { return Paging1.PageSize; }
        set { Paging1.PageSize = value; }
    }

    protected int _StartIndex;
    public int StartIndex
    {
        get { return _StartIndex; }
        set { _StartIndex = value; }
    }

    public bool Paging
    {
        get { return Paging1.Visible; }
        set { Paging1.Visible = value; }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Session[Constants.SessionUserId] != null)
                currentID.Value = Session[Constants.SessionUserId].ToString();
            else
            { 
                HttpCookie _cookie = Request.Cookies["InfoLogin"];
                if (_cookie != null)
                {
                    string[] strInfo = _cookie.Value.Split(',');
                    if (strInfo.Length > 0)
                    {
                        DataTable _dtRole = _TblThanhVienManager.GetIDByUserName(strInfo[0]);
                        if (_dtRole.Rows.Count > 0)
                            currentID.Value = _dtRole.Rows[0][0].ToString();
                    }
                }
            }
            Paging1.CurrentPage = 1;
            FunctionExtend.LoadPageSize(ddlPageSize);
            LoadDataToGrid(new TblThanhVienEntity());
        }
    }

    private void LoadDataToGrid(TblThanhVienEntity _TblThanhVienEntity)
    {
        Paging1.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue);
        _TblThanhVienEntity.LangId = LangID;
        _TblThanhVienEntity.StartIndex = (Paging1.CurrentPage - 1) * Paging1.PageSize;
        _TblThanhVienEntity.EndIndex = Paging1.CurrentPage * Paging1.PageSize;
        if (Paging1.CurrentPage > 1) _TblThanhVienEntity.StartIndex += 1;
        DataTable dt = _TblThanhVienManager.be_selectToGridView(_TblThanhVienEntity);
        if (dt != null)
        {
            if (dt.Rows.Count > 0)
                Paging1.TotalItems = Convert.ToInt32(dt.Rows[0]["TotalItems"].ToString());
            dgvListRole.DataSource = dt;
            dgvListRole.DataBind();
            lblTotalRecord.Text = dt.Rows.Count.ToString();
        }
        if (Paging1.Visible) Paging1.BeginPaging();

    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/AdminPortal/Pages/UserAdd.aspx");
    }

    protected void btnDel_Click(object sender, EventArgs e)
    { 
        
        foreach (GridViewRow row in dgvListRole.Rows)
        {
            HtmlInputCheckBox chkID = (HtmlInputCheckBox)row.FindControl("chkID");
            if (chkID.Checked)
            {
                if (chkID.Value.Equals(currentID.Value)) continue; //khong cho xoa chinh minh
                try
                {
                    _TblThanhVienManager.DeleteById(Convert.ToInt32(chkID.Value));
                }
                catch { }
            }
        }
        Paging1.CurrentPage = 1;
        LoadDataToGrid(new TblThanhVienEntity());
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        TblThanhVienEntity _TblThanhVienEntity = new TblThanhVienEntity();
        _TblThanhVienEntity.Condition = txtKeySearch.Text;
        _TblThanhVienEntity.LangId = LangID;
        Paging1.CurrentPage = 1;
        LoadDataToGrid(_TblThanhVienEntity);

    }
    protected void dgvListRole_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        DataRowView r = (DataRowView)e.Row.DataItem;

        HyperLink lnkUserName = (HyperLink)e.Row.FindControl("lnkUserName");

        HyperLink lnkDoiMatKhau = (HyperLink)e.Row.FindControl("lnkDoiMatKhau");

        HyperLink lnkPhanQuyen = (HyperLink)e.Row.FindControl("lnkPhanQuyen");

        if (r != null && lnkUserName != null && lnkDoiMatKhau != null && lnkPhanQuyen!=null)
        {
            lnkUserName.Text = r[TblThanhVienFields.TenTruyCap.Name].ToString();

            lnkUserName.NavigateUrl = "~/AdminPortal/Pages/UserEdit.aspx?id=" + r[TblThanhVienFields.Id.Name].ToString();

            lnkDoiMatKhau.NavigateUrl = "~/AdminPortal/Pages/ChangePass.aspx?id=" + r[TblThanhVienFields.Id.Name].ToString();

            lnkPhanQuyen.NavigateUrl = "~/AdminPortal/Pages/RoleList.aspx?id=" + r[TblThanhVienFields.Id.Name].ToString();
        }
    }
    protected void Paging1_OnPageIndexChanged(object sender, EventArgs e)
    {
        LoadDataToGrid(new TblThanhVienEntity());
        Paging1.BeginPaging();
    }
    protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
    {
        Paging1.CurrentPage = 1;
        LoadDataToGrid(new TblThanhVienEntity());
    }
}